//
//  VisitorTableViewController.swift
//  MicroBlog
//
//  Created by 002 on 15/10/15.
//  Copyright © 2015年 002. All rights reserved.
//

import UIKit

class VisitorTableViewController: UITableViewController {

    // 用户是否登录的标志
    private var isLogin : Bool = UserAccountViewModel.shareUserAccount.isUserLogin
    
    private var webView : UIWebView?
    
    // 对外部提供访客视图对象，用于设置访客视图的特有信息
    var visitor : VisitorView?
    
    override func viewDidLoad() {
        
        // 添加导航栏按钮
        addNavigationItemBtn()
    }
    
    //  This method loads or creates a view and assigns it to the view property.
    //  设置当前控制器的View视图
    override func loadView() {
        
        isLogin ? super.loadView() : setupVisitorView()
    }
    
    //MARK: 导航栏按钮添加方法
    func addNavigationItemBtn() {
        
        // 创建导航栏的‘注册’、‘登录’按钮
        let registerItem = UIBarButtonItem.init(title: "注册", style: UIBarButtonItemStyle.Plain, target: self, action: "VisitorViewDidRigister")
        let loginItem = UIBarButtonItem.init(title: "登陆", style: UIBarButtonItemStyle.Plain, target: self, action: "VisitorViewDidLogin")
        
        self.navigationItem.leftBarButtonItem = registerItem
        self.navigationItem.rightBarButtonItem = loginItem
    }
}


// MARK: - 判断是否设置访客视图
extension VisitorTableViewController {

    // 设置访客视图
    private func setupVisitorView() {
        
        // 实例化访客视图(创建视图的时候已经在构造函数里面完成了各个控制器特有访问视图的加载)
        self.visitor = VisitorView()

        // 替换视图
        self.view = self.visitor
        
        // 对‘登录’ ‘注册’ 按钮添加监听
        self.visitor?.registerBtn.addTarget(self, action: "VisitorViewDidRigister", forControlEvents: UIControlEvents.TouchUpInside)
        self.visitor?.loginBtn.addTarget(self, action: "VisitorViewDidLogin", forControlEvents: UIControlEvents.TouchUpInside)
    }
}

// MARK: - 登陆和注册按钮的‘监听’方法
extension VisitorTableViewController {
    
    func VisitorViewDidRigister() {
        print("注册")
    }
    
    func VisitorViewDidLogin() {
        print("登陆")
        let vc = OAuthViewController()
        let nvc = UINavigationController(rootViewController: vc)
        
        self.presentViewController(nvc, animated: true, completion: nil)
        
    }
    
}
